import {UserRepository} from '../repository/user';
import {User} from '../entity/user';
import {getRepository} from "typeorm";

// 获取存储库
// @Injectable()
class UserService {

    private userRepository: UserRepository;

    constructor() {
        // this.userRepository = new UserRepository(User, manager);
        this.userRepository = getRepository(User);
    }

    async findAll(): Promise<User[]> {
        return this.userRepository.find({
            relations: {
                tasks: true,
            }
        });
    }

    async findOne(id: number): Promise<User | null> {
        return this.userRepository.findOneBy({"id": id});
    }

    async create(data: object): Promise<User> {
        const user = this.userRepository.create(data);
        return this.userRepository.save(user);
    }

    async update(id: number, data: object): Promise<User | undefined> {
        return this.userRepository.save({id, ...data})
    }

    async delete(id: number): Promise<void> {
        this.userRepository.delete(id);
    }
}

export default UserService;
